package vn.info.vietnaminfo.adapter;

import java.util.List;

import vn.info.vietnaminfo.R;
import vn.info.vietnaminfo.VietNamInfoActivity;
import vn.info.vietnaminfo.data.DSThanhPho;
import vn.info.vietnaminfo.utli.GlobalVariable;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.TextView;

public class ThanhPhoAdapter extends BaseAdapter
{
	private Context mContext;
	private LayoutInflater mInflater;	
	private List<DSThanhPho> listThanhPho = null;
	private String txtMaTP = "";
	
	public ThanhPhoAdapter(Context context, List<DSThanhPho> object)
	{
		mContext  	 = context;
		listThanhPho = object;
		mInflater 	 = LayoutInflater.from(context);
	}
	
	public class ViewHolder
	{
		TextView txtTenThanhPho;
	}

	public View getView(final int position, View view, ViewGroup parent)
	{
		final ViewHolder holder;
		if (view == null)
		{
			holder 					= new ViewHolder();
			view 					= mInflater.inflate(R.layout.layout_item_city, null);
			holder.txtTenThanhPho	= (TextView)view.findViewById(R.id.txt_item_ten_tp);
			view.setTag(holder);
		}
		else
		{
			holder = (ViewHolder) view.getTag();
		}
		holder.txtTenThanhPho.setTag(position);
		
		if(txtMaTP.trim().equals(listThanhPho.get(position).getMaTP()))
		{
			holder.txtTenThanhPho.setSelected(true);
		}
		else
		{
			holder.txtTenThanhPho.setSelected(false);
		}
		
		holder.txtTenThanhPho.setText(listThanhPho.get(position).getTenTP());
		holder.txtTenThanhPho.setOnClickListener(new OnClickListener()
		{
			public void onClick(View v)
			{
				GlobalVariable.LATITUDE = Double.valueOf(listThanhPho.get(position).getLatitude());
				GlobalVariable.LONGITUDE = Double.valueOf(listThanhPho.get(position).getLongitude());
				
				txtMaTP = listThanhPho.get(position).getMaTP();
				holder.txtTenThanhPho.setSelected(true);
				notifyDataSetChanged();
				((VietNamInfoActivity)mContext).dongDanhSachThanhPho();
			}
		});
		
		return view;
	}
	
	public int getCount()
	{
		return listThanhPho.size();
	}

	public Object getItem(int position)
	{
		return position;
	}

	public long getItemId(int position)
	{
		return position;
	}
}